package com.juzipi.demo.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.juzipi.demo.dto.TeacherStudent;
import com.juzipi.demo.mapper.StudentMapper;
import com.juzipi.demo.mapper.SubjectMapper;
import com.juzipi.demo.mapper.TeacherMapper;
import com.juzipi.demo.pojo.PageResult;
import com.juzipi.demo.pojo.Subject;
import com.juzipi.demo.pojo.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeacherService {


    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private SubjectMapper subjectMapper;
    @Autowired
    private StudentMapper studentMapper;



    public Teacher queryTeacher(Integer id) {

        return teacherMapper.selectByPrimaryKey(id);

    }


    public String addTeacher(Teacher teacher) {

        int i = teacherMapper.insertSelective(teacher);
        if (i == 1){
            return "新增成功";
        }
        return "新增失败";

    }



    @Transactional
    public String updateTeacher(Teacher teacher) {

        int i = teacherMapper.updateByPrimaryKeySelective(teacher);
        if (i == 1){
            return "修改成功";
        }
        return "修改失败";


    }

    public String deleteTeacherById(Integer id) {

        int i = teacherMapper.deleteByPrimaryKey(id);
        if (i == 1){
            return "删除成功";
        }
        return "删除失败";
    }


    public PageResult<Teacher> queryTeacherSubject(String key, Integer pageNum, Integer pageSize) {

        List<Teacher> teachers = teacherMapper.queryTeacherAndSubject(key);
        Subject subject = subjectMapper.selectByPrimaryKey(1);

        for (Teacher teacher: teachers) {
            teacher.setSubject(subject);
        }

        PageHelper.startPage(pageNum,pageSize);

        PageInfo<Teacher> pageInfo = new PageInfo<>(teachers);

        return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());

    }



    public Map<String, Object> queryTeacherStudent(String key, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum,pageSize);
        List<TeacherStudent> teacherList = teacherMapper.queryTeacherStudent(key);
        Teacher teacher = teacherMapper.selectByPrimaryKey(1);
        PageInfo<TeacherStudent> pageInfo = new PageInfo<>(teacherList);

        HashMap<String, Object> hashMap = new HashMap<>();
        //老师的名字做键，学生做值
        hashMap.put("总计多少条数据",pageInfo.getTotal());
        hashMap.put("页面大小",pageInfo.getSize());
        hashMap.put("当前页码",pageInfo.getPageNum());
        for (TeacherStudent teacherStudent: teacherList) {
            //没必要了，结果在map里
//            hashMap.put(teacherStudent.getTname(),teacherStudent.getTgender());
            hashMap.put(teacher.getName(),pageInfo.getList());
        }

        return hashMap;
    }
}
